import {ChangeDetectionStrategy, ChangeDetectorRef, Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup} from "@angular/forms";
import {NzMessageService} from "ng-zorro-antd/message";
import {ValidatorsService} from "../../../../core/services/validators/validators.service";
import {IdentifyIdService} from "../../../../core/services/store/biz-store-service/identify-id/identify-id.service";
import {fnCheckForm} from "../../../../utils/tools";
import {AdminService} from "../../../../core/services/store/admin.service";
import {MajorHazardSourcesService} from "../../../../core/services/http/classification-identification/major-hazard-sources.service";

@Component({
  selector: 'app-major-hazard-sources',
  templateUrl: './major-hazard-sources.component.html',
  styleUrls: ['./major-hazard-sources.component.less'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class MajorHazardSourcesComponent implements OnInit {
  validateForm!: FormGroup;
  formOrder = 1;
  identifyId!: number;
  id!: number;

  constructor(private fb: FormBuilder, private msg: NzMessageService,
              private validatorsService: ValidatorsService,
              private cdr: ChangeDetectorRef,
              private identifyIdService: IdentifyIdService, public AdminService: AdminService,
              private dataService: MajorHazardSourcesService) {
  }

  initForm(): void {
    this.validateForm = this.fb.group({
      dangerMatterKey: [null],
      dangerType: [null],
      symbol: [null],
      beta: [null],
      thresholdQuantity: [null],
    });
  }

  submitForm(): void {
    if (!fnCheckForm(this.validateForm)) {
      return;
    }
    const params = this.validateForm.value;
    params.id = this.id;
    this.dataService.editMajorHazardSources(params).subscribe()

  }

  getDetail(identifyId: number): void {
    this.dataService.getMajorHazardSourcesDetail(identifyId).subscribe(res => {
      this.id = res.id as number;
      this.validateForm.patchValue(res);
    })
  }


  ngOnInit(): void {
    this.initForm();
    this.identifyIdService.getIdentifyIdStore().subscribe(res => this.identifyId = res);
    this.getDetail(this.identifyId)
  }

}
